package com.fingard.dsp.bank.directbank.bbg02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2020-04-21.
 * @version 1.0
 */
public class BBG02Base extends DirectBase implements ITcpClient {
    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime12Year2Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

    //scoket格式
    protected String[] sendToBank(String p_reqStr, String transType, String commandCode) throws Exception {
        String[] retStr = new String[]{"", ""};

        if (StringHelper.hasAnyChar(transType) && StringHelper.hasAnyChar(commandCode)) {
            this.detailSimulateForBank(bankFront.bankCode, transType, commandCode, p_reqStr, retStr);
            if (retStr[1].length() > 0) {
                return retStr;
            }
        }

        int tmpReqBodyLen = p_reqStr.getBytes(getCharset()).length + 2;
        String tmpReqHeader = String.format("%-10s", tmpReqBodyLen + "") + "00";
        p_reqStr = tmpReqHeader + p_reqStr;

        WriteBankUrlLogLn("请求银行的地址" + bankFront.serverIP + ":" + bankFront.serverPort);
        WriteBankLogStep2(p_reqStr);
        //发送银行前置
        TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
        tmpClient.timeoutMil = 300000;
        TcpReceiver tmpReceiver = tmpClient.sendMessage(p_reqStr, getCharset());
        retStr[0] = tmpReceiver.connState;
        retStr[1] = tmpReceiver.getFullString(getCharset());
        if (StringHelper.isNullOrEmpty(retStr[1])) {
            retStr[1] = tmpReceiver.getConnMsg();
        }
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, BBG02Header p_bankHeader) {
        //xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("ap");
        xmlWriter.writeStartElement("head");
        xmlWriter.writeElementString("tr_code", p_bankHeader.tr_code);
        xmlWriter.writeElementString("cms_corp_no", "");//银企直连客户号
        xmlWriter.writeElementString("user_no", "");//用户号
        xmlWriter.writeElementString("org_code", "");//机构号
        xmlWriter.writeElementString("serial_no", "");//交易流水号
        xmlWriter.writeElementString("req_no", p_bankHeader.req_no);//请求号
        xmlWriter.writeElementString("tr_acdt", Format.Date8Format.format(new Date()));//交易日期
        xmlWriter.writeElementString("tr_time", Format.Time6Format.format(new Date()));//交易时间
        xmlWriter.writeElementString("channel", "5");//渠道标识
        xmlWriter.writeElementString("sign", "");//签名标识
        xmlWriter.writeElementString("file_flag", p_bankHeader.file_flag);//传送0-报文1-文件
        xmlWriter.writeElementString("reserved", "");//备用字段
        xmlWriter.writeEndElement();
    }


    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            String tmpCharset = getCharset();
            String tmpRecNow = new String(p_receiver.recBytes, 0, p_receiver.readedLength, tmpCharset);
            if (tmpRecNow.contains("</ap>")) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }
}
